﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{
    public class RestDivisionDataAccess : OrganizationalDivisionsDataAccess
    {
        public RestDivisionDataAccess(string dbName, string conn, bool usePredefinedMetadata)
            :base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<RestDivision> SelectRestDivisionsList()
        {
            try
            {
                //Select OrganizationalDivision List
                List<OrganizationalDivision> orgDivList = SelectOrganizationalDivisionsList();

                //Select RestDivisions List
                List<REST_DIVISION> restDivisionList = dataContext.REST_DIVISION.ToList();

                //Merge result
                var res = (from orgDiv in orgDivList
                           join restDivision in restDivisionList on orgDiv.ID equals restDivision.ID
                           select new RestDivision
                          {
                              DataBaseName = dataBaseName,
                              CLASS_ID = orgDiv.CLASS_ID,
                              EXT_ID = orgDiv.EXT_ID,
                              ID = orgDiv.ID,
                              NAME = orgDiv.NAME,
                              PARENT_ID = orgDiv.PARENT_ID,
                              BOSS_ID = orgDiv.BOSS_ID,
                              DATE_CREATED = orgDiv.DATE_CREATED,
                              DESCRIPTION = orgDiv.DESCRIPTION,
                              EMAIL = orgDiv.EMAIL,
                              SLOGAN = orgDiv.SLOGAN,
                              LOCATION_ID = restDivision.LOCATION_ID,
                              TELEPHONE = restDivision.TELEPHONE
                          }).ToList();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectRestDivisionBy_Id");
            }
        }

        public RestDivision SelectRestDivisionBy_Id(int Id)
        {
            try
            {
                //Select OrgDiv part by Id
                OrganizationalDivision orgDivPart = SelectOrganizationalDivisionBy_Id(Id);

                //Select RestDivision part
                List<REST_DIVISION>  restDivisionPart = dataContext.REST_DIVISION.ToList();

                //Merge result
                var res = (from restDivision in restDivisionPart
                           where restDivision.ID == orgDivPart.ID
                           select new RestDivision
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = orgDivPart.CLASS_ID,
                               EXT_ID = orgDivPart.EXT_ID,
                               ID = orgDivPart.ID,
                               NAME = orgDivPart.NAME,
                               PARENT_ID = orgDivPart.PARENT_ID,
                               BOSS_ID = orgDivPart.BOSS_ID,
                               DATE_CREATED = orgDivPart.DATE_CREATED,
                               DESCRIPTION = orgDivPart.DESCRIPTION,
                               EMAIL = orgDivPart.EMAIL,
                               SLOGAN = orgDivPart.SLOGAN,
                               LOCATION_ID = restDivision.LOCATION_ID,
                               TELEPHONE = restDivision.TELEPHONE
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectRestDivisionBy_Id");
            }
        }

        public void InsertRestDivision(RestDivision restDivision, ref int generated_Id)
        {
            try
            {
                //Insert the OrgDivision part
                InsertOrganizationalDivision(restDivision, ref generated_Id);

                //Insert the RestDivision part
                int new_Id = generated_Id;
                REST_DIVISION restDivisionPart = new REST_DIVISION();
                restDivisionPart.ORGANIZATIONAL_DIVISION = dataContext.ORGANIZATIONAL_DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                restDivisionPart.ID = new_Id;
                restDivisionPart.LOCATION_ID = restDivision.LOCATION_ID;
                restDivisionPart.TELEPHONE = restDivision.TELEPHONE;
               
                //Submit changes
                dataContext.AddToREST_DIVISION(restDivisionPart);
                dataContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw new Exception("Error while InsertRestDivision");
            }
        }

        public void UpdateRestDivision(RestDivision restDivision)
        {
            try
            {
                //Update the OrganizationalDivision part
                UpdateOrganizationalDivision(restDivision);

                //Update the RestDivision part
                REST_DIVISION restDivisionPart = dataContext.REST_DIVISION.Where(t => t.ID == restDivision.ID).FirstOrDefault();
                restDivisionPart.LOCATION_ID = restDivision.LOCATION_ID;
                restDivisionPart.TELEPHONE = restDivision.TELEPHONE;
                               
                //Submit changes
                dataContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw new Exception("Error while UpdateRestDivision");
            }
        }

        public void DeleteRestDivision(RestDivision restDivision)
        {
            try
            {
                //Delete the RestDivision part
                REST_DIVISION divPart = dataContext.REST_DIVISION.Where(t => t.ID == restDivision.ID).FirstOrDefault();

                dataContext.DeleteObject(divPart);
                dataContext.SaveChanges();

                //Delete the OrganizationalDivision part
                DeleteOrganizationalDivision(restDivision);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteRestDivision");
            }
        }
    }
}
